﻿CREATE PROCEDURE dbo.smenu_Tracker_License_IfSys
(	@Counter int,
	@Count int
)AS
SET NOCOUNT ON

DECLARE @TrackerID sysname SET @TrackerID = dbo.TrackerByCounter(@Counter)

DECLARE @TechID int,@BranchID int
SELECT TOP 1 @TechID = tch.TechnicianID,
						 @BranchID = d.BranchID
FROM vhist_t_Tracker_Car tch
INNER JOIN t_TrackerRequest tr ON tch.TrackerRequestID = tr.ID
INNER JOIN t_Dealer d ON tr.DEalerID = d.ID
WHERE (tch.TrackerID = @TrackerID)
ORDER BY tch.HID DESC

IF @TechID IS NULL
SELECT TOP 1 @TechID = tch.TechnicianID,
						 @BranchID = d.BranchID
FROM vhist_t_Tracker_Car tch
INNER JOIN t_TrackerRequest tr ON tch.VIN = tr.VIN
INNER JOIN t_Dealer d ON tr.DEalerID = d.ID
WHERE (tch.TrackerID = @TrackerID)
ORDER BY tch.HID DESC

IF @TechID >= 0 RETURN

EXEC s_Branch_Licenses_Add @Count,@BranchID


